package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import cjgl.dao.ConnectionFactory;
import cjgl.dao.CounterDAO;

/**
 * Application Lifecycle Listener implementation class ContextListener
 *
 */
@WebListener
public class ContextListener implements ServletContextListener {
   private CounterDAO counterDAO=new CounterDAO();
    /**
     * Default constructor. 
     */
    public ContextListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce)  { 
         ServletContext ctxt=sce.getServletContext();
         Integer count=(Integer)ctxt.getAttribute("count");
         counterDAO.setValue(count);
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce)  { 
         ServletContext sctext =sce.getServletContext();
         String dbName =sctext.getInitParameter("dbName");
         String username =sctext.getInitParameter("username");
         String password =sctext.getInitParameter("pwd");
         if(dbName!=null) {
        	 ConnectionFactory.setDbName(dbName);
         }
         if(username!=null) {
        	 ConnectionFactory.setUsername(username);
         }
         if(password!=null) {
        	 ConnectionFactory.setPassword(password);
         }
    }
    
    
    
	
}
